//
//  NearbyMainViewController.m
//  HappyTrip
//
//  Created by yuxuesong on 15-3-22.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "NearbyMainViewController.h"

#import "NearbyListViewController.h"




//签订CoreLocationManager的代理
@interface NearbyMainViewController ()<CLLocationManagerDelegate>

@property (nonatomic, retain)CLLocationManager *locationManager;
@property (nonatomic, retain)CLLocation *loc;

@end

@implementation NearbyMainViewController

- (void)dealloc
{
    [_loc release];
    [_locationManager release];
    [super dealloc];
}

- (void)locationMethod
{
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    if ([CLLocationManager locationServicesEnabled]) {
        self.locationManager.distanceFilter = 100;//设置为100米
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [self.locationManager requestWhenInUseAuthorization];//或者下一句
        //[self.locationManager requestAlwaysAuthorization];
        [self.locationManager startUpdatingLocation];
        NSLog(@"111111");
        [_locationManager release];
    } else {
        NSLog(@"请检查网络");
    }
}

//   实现CLLocationManagerDelegate的代理方法
//   获取到位置数据，返回的是一个CLLocation的数组，一般使用其中的一个
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    NSLog(@"222222");
    self.loc = [locations lastObject];
    NSLog(@"纬度 = %f, 经度 = %f", self.loc.coordinate.latitude, self.loc.coordinate.longitude);
}

//获取用户位置数据失败的回调方法，在此通知用户
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"333333");
    NSLog(@"%@", error);
    if ([error code] == kCLErrorDenied) {
        NSLog(@"访问被拒绝");
    }else if ([error code] == kCLErrorLocationUnknown){
        NSLog(@"无法获取位置信息");
        static NSInteger i = 1;
        if (i == 1) {
            UIAlertView *theAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"无法获取位置信息,推荐您去云南看看吧" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [theAlertView show];
            [self.view addSubview:theAlertView];
            [theAlertView release];
        }
        i++;
    }
}

//离开页面时,关闭定位
//- (void)viewDidDisappear:(BOOL)animated
//{
//    [super viewDidDisappear:animated];
//    [self.locationManager stopUpdatingLocation];
//}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    //调用定位方法
    [self locationMethod];
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0 / 255.0 green:170.0 / 255.0 blue:205.0 / 255.0 alpha:1.0];
    self.tabBarController.tabBar.hidden = NO;
    self.view.backgroundColor = [UIColor colorWithRed:250.0 / 255.0 green:240.0 / 255.0 blue:220.0 / 255.0 alpha:1.0];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    



    

    self.view.backgroundColor = [UIColor colorFromHexCode:@"#F5FFFA"];

     self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0 / 255.0 green:170.0 / 255.0 blue:205.0 / 255.0 alpha:1.0];
    

    
    //景点
    UIButton *sceButton = [UIButton buttonWithTitle:nil frame:CGRectMake(0, 1, (VIEW_WIDTH) / 2, (VIEW_HEIGHT - 30) / 2 - 1) target:self action:@selector(sceButtonAction:)];
    [sceButton setImage:[UIImage imageNamed:@"sceWa.png"] forState:UIControlStateNormal];
    [self.view addSubview:sceButton];

    
    //休闲娱乐
    UIButton *relButton = [UIButton buttonWithTitle:nil frame:CGRectMake(0, (VIEW_HEIGHT - 30) / 2 + ((VIEW_HEIGHT - (VIEW_HEIGHT - 30) / 2)) / 2, VIEW_WIDTH, ((VIEW_HEIGHT - (VIEW_HEIGHT - 30) / 2)) / 2  - 1) target:self action:@selector(relButtonAction:)];
    [relButton setImage:[UIImage imageNamed:@"relWa-1.png"] forState:UIControlStateNormal];
    [self.view addSubview:relButton];

    
    //购物
    UIButton *shoButton = [UIButton buttonWithTitle:nil frame:CGRectMake((VIEW_WIDTH) / 2 + 1, ((VIEW_HEIGHT - (VIEW_HEIGHT - 30) / 2 - 1)) / 2 - 2 + 2, (VIEW_WIDTH) / 2 - 1, (VIEW_HEIGHT - 30) / 2) target:self action:@selector(shoButtonAction:)];
    [shoButton setImage:[UIImage imageNamed:@"shoWa.png"] forState:UIControlStateNormal];
    [self.view addSubview:shoButton];

    
    //餐厅
    UIButton *resButton = [UIButton buttonWithTitle:nil frame:CGRectMake((VIEW_WIDTH) / 2 + 1, 1, (VIEW_WIDTH) / 2 , ((VIEW_HEIGHT - (VIEW_HEIGHT - 30) / 2 - 1)) / 2 - 2) target:self action:@selector(resButtonAction:)];
    [resButton setImage:[UIImage imageNamed:@"resWa.png"] forState:UIControlStateNormal];
    [self.view addSubview:resButton];

    
    //住宿
    UIButton *hotButton = [UIButton buttonWithTitle:nil frame:CGRectMake(0, (1 + (VIEW_HEIGHT - 30) / 2 - 1) + 1, (VIEW_WIDTH / 2), ((VIEW_HEIGHT - (VIEW_HEIGHT - 30) / 2)) / 2 - 2) target:self action:@selector(hotButtonAction:)];
    [hotButton setImage:[UIImage imageNamed:@"hotWa.png"] forState:UIControlStateNormal];
    [self.view addSubview:hotButton];

    

}

- (void)sceButtonAction:(UIButton *)button
{
//    NSLog(@"景点11");
    NearbyListViewController *listVC = [[NearbyListViewController alloc] init];
    listVC.urlID = [NSString stringWithFormat:@"%d", 11];
    //传值,传经纬度
    listVC.lat = [NSString stringWithFormat:@"%f", self.loc.coordinate.latitude];
    listVC.lng = [NSString stringWithFormat:@"%f", self.loc.coordinate.longitude];
    listVC.title = @"景点";
    [self.navigationController pushViewController:listVC animated:YES];
    [listVC release];
}

- (void)relButtonAction:(UIButton *)button
{
//    NSLog(@"休闲娱乐21");
    NearbyListViewController *listVC = [[NearbyListViewController alloc] init];
    listVC.urlID = [NSString stringWithFormat:@"%d", 21];
    //传值,传经纬度
    listVC.lat = [NSString stringWithFormat:@"%f", self.loc.coordinate.latitude];
    listVC.lng = [NSString stringWithFormat:@"%f", self.loc.coordinate.longitude];
    listVC.title = @"休闲娱乐";
    [self.navigationController pushViewController:listVC animated:YES];
    [listVC release];
    
}

- (void)shoButtonAction:(UIButton *)button
{
//    NSLog(@"购物6");
    NearbyListViewController *listVC = [[NearbyListViewController alloc] init];
    listVC.urlID = [NSString stringWithFormat:@"%d", 6];
    //传值,传经纬度
    listVC.lat = [NSString stringWithFormat:@"%f", self.loc.coordinate.latitude];
    listVC.lng = [NSString stringWithFormat:@"%f", self.loc.coordinate.longitude];
    listVC.title = @"购物";
    [self.navigationController pushViewController:listVC animated:YES];
    [listVC release];
    
}

- (void)resButtonAction:(UIButton *)button
{
//    NSLog(@"餐厅5");
    NearbyListViewController *listVC = [[NearbyListViewController alloc] init];
    listVC.urlID = [NSString stringWithFormat:@"%d", 5];
    //传值,传经纬度
    listVC.lat = [NSString stringWithFormat:@"%f", self.loc.coordinate.latitude];
    listVC.lng = [NSString stringWithFormat:@"%f", self.loc.coordinate.longitude];
    listVC.title = @"餐厅";
    [self.navigationController pushViewController:listVC animated:YES];
    [listVC release];
    
}

- (void)hotButtonAction:(UIButton *)button
{
//    NSLog(@"住宿10");
    NearbyListViewController *listVC = [[NearbyListViewController alloc] init];
    listVC.urlID = [NSString stringWithFormat:@"%d", 10];
    //传值,传经纬度
    listVC.lat = [NSString stringWithFormat:@"%f", self.loc.coordinate.latitude];
    listVC.lng = [NSString stringWithFormat:@"%f", self.loc.coordinate.longitude];
    listVC.title = @"住宿";
    [self.navigationController pushViewController:listVC animated:YES];
    [listVC release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
